﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuanLyKho.Server;

namespace QuanLyKho.Server.BLL
{
    public class Dm_ThuocBLL
    {
        public Dm_ThuocBLL()
        {
        }

        public List<Dm_Thuoc> RetrieveAll()
        {
            List<Dm_Thuoc> list = new List<Dm_Thuoc>();
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                list = entities.Dm_Thuoc.Include("Dm_HamLuong").Include("Dm_DonViTinh").Include("Dm_QuyCach").Where(u => u.KichHoat && !u.DaXoa).ToList();
            }
            return list;
        }

        public Dm_Thuoc RetrieveById(string maThuoc)
        {
            Dm_Thuoc dmThuoc;
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                dmThuoc = entities.Dm_Thuoc.Include("Dm_HamLuong").Include("Dm_DonViTinh").Include("Dm_QuyCach").Where(u => u.MaThuoc.StartsWith(maThuoc) && u.KichHoat && !u.DaXoa).FirstOrDefault();
            }
            return dmThuoc;
        }

        public Dm_Thuoc RetrieveByTenThuocHamLuong(string tenThuocHamLuong)
        {
            Dm_Thuoc dmThuoc;
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                dmThuoc = entities.Dm_Thuoc.Include("Dm_HamLuong").Include("Dm_DonViTinh").Include("Dm_QuyCach").Where(u => u.TenThuocHamLuong.StartsWith(tenThuocHamLuong) && u.KichHoat && !u.DaXoa).FirstOrDefault();
            }
            return dmThuoc;
        }

        public Dm_Thuoc RetrieveByTenThuoc(string tenThuoc)
        {
            Dm_Thuoc dmThuoc;
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                dmThuoc = entities.Dm_Thuoc.Include("Dm_HamLuong").Include("Dm_DonViTinh").Include("Dm_QuyCach").Where(u => u.TenThuoc.StartsWith(tenThuoc) && u.KichHoat && !u.DaXoa).FirstOrDefault();
            }
            return dmThuoc;
        }

        public Dm_Thuoc AddNewDmThuoc(Dm_Thuoc objectDmThuoc)
        {
            Dm_Thuoc dmThuocTemple = new Dm_Thuoc();
            int error = -1;
            try
            {
                dmThuocTemple = objectDmThuoc;
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    entities.Dm_Thuoc.Add(dmThuocTemple);
                    error = entities.SaveChanges();
                    if (error < 0)
                    {
                        dmThuocTemple = null;
                    }
                }
            }
            catch
            {
                dmThuocTemple = null;
            }
            return dmThuocTemple;
        }

        public bool UpdateDmThuoc(Dm_Thuoc objectDmThuoc)
        {
            Dm_Thuoc dmThuocTemple = new Dm_Thuoc();
            int error = -1;
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    dmThuocTemple = entities.Dm_Thuoc.SingleOrDefault(c => c.MaThuoc == objectDmThuoc.MaThuoc);
                    dmThuocTemple.TenThuoc = objectDmThuoc.TenThuoc;
                    dmThuocTemple.MaDonViTinh = objectDmThuoc.MaDonViTinh;
                    dmThuocTemple.GiaNhap = objectDmThuoc.GiaNhap;
                    dmThuocTemple.GiaXuat = objectDmThuoc.GiaXuat;
                    dmThuocTemple.MaQuyCach = objectDmThuoc.MaQuyCach;
                    dmThuocTemple.MaHamLuong = objectDmThuoc.MaHamLuong;
                    dmThuocTemple.SoDK = objectDmThuoc.SoDK;
                    dmThuocTemple.DaXoa = objectDmThuoc.DaXoa;
                    dmThuocTemple.KichHoat = objectDmThuoc.KichHoat;
                    error = entities.SaveChanges();
                    if (error >= 0)
                    {
                        return true;
                    }
                }
            }
            catch
            {
            }
            return false;
        }

        public bool DeleteDmThuoc(Dm_Thuoc objectDmThuoc)
        {
            Dm_Thuoc dmThuocTemple = new Dm_Thuoc();
            int error = -1;
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    dmThuocTemple = entities.Dm_Thuoc.SingleOrDefault(c => c.MaThuoc == objectDmThuoc.MaThuoc);
                    entities.Dm_Thuoc.Remove(dmThuocTemple);
                    error = entities.SaveChanges();
                    if (error >= 0)
                    {
                        return true;
                    }
                }
            }
            catch
            {
            }
            return false;
            //Dm_Thuoc dmThuocTemple = new Dm_Thuoc();
            //int error = -1;
            //try
            //{
            //    using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            //    {
            //        dmThuocTemple = entities.Dm_Thuoc.SingleOrDefault(c => c.MaThuoc == objectDmThuoc.MaThuoc);
            //        dmThuocTemple.DaXoa = true;
            //        error = entities.SaveChanges();
            //        if (error >= 0)
            //        {
            //            return true;
            //        }
            //    }
            //}
            //catch
            //{
            //}
            //return false;
        }

        public List<Dm_Thuoc> ListDmThuoc()
        {
            List<Dm_Thuoc> dmThuocTemple = new List<Dm_Thuoc>();
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    dmThuocTemple = (from c in entities.Dm_Thuoc
                                     .Include("Dm_DonViTinh")
                                     .Include("Dm_QuyCach")
                                     .Include("Dm_HamLuong")
                                     select c).ToList();
                }
            }
            catch
            {
            }
            return dmThuocTemple;
        }

        public List<Dm_Thuoc> FindListDmThuoc(string maThuoc, string tenThuoc)
        {
            List<Dm_Thuoc> dmThuocTemple = new List<Dm_Thuoc>();
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    if (!string.IsNullOrEmpty(maThuoc))
                    {
                        dmThuocTemple = (from c in entities.Dm_Thuoc
                                       where c.MaThuoc.Contains(maThuoc)
                                       select c).ToList();
                    }
                    else if (!string.IsNullOrEmpty(tenThuoc))
                    {
                        dmThuocTemple = (from c in entities.Dm_Thuoc
                                       where c.MaThuoc.Contains(tenThuoc)
                                       select c).ToList();
                    }
                    else
                    {
                        dmThuocTemple = (from c in entities.Dm_Thuoc
                                       where c.MaThuoc.Contains(maThuoc)
                                             && c.MaThuoc.Contains(tenThuoc)
                                       select c).ToList();
                    }
                }
            }
            catch
            {
            }
            return dmThuocTemple;
        }

        public bool FindTrungMaDmThuoc(string maThuoc)
        {
            Dm_Thuoc dmThuocTemple = new Dm_Thuoc();
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    dmThuocTemple = entities.Dm_Thuoc.SingleOrDefault(c => c.MaThuoc == maThuoc);
                    if (dmThuocTemple != null)
                    {
                        return true;
                    }
                }
            }
            catch
            {
            }
            return false;
        }

        public bool FindMaThuocInNhapChiTietThuoc(string maThuoc)
        {
            PhieuNhapThuocChiTiet dmThuocTemple = new PhieuNhapThuocChiTiet();
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    dmThuocTemple = entities.PhieuNhapThuocChiTiets.SingleOrDefault(c => c.MaThuoc == maThuoc);
                    if (dmThuocTemple != null)
                    {
                        return true;
                    }
                }
            }
            catch
            {
            }
            return false;
        }
    }
}
